package jdbc;

import java.sql.*;
import java.util.Scanner;

public class HomeWorkDemo2 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入用户名");
        String username = scan.nextLine();
        System.out.println("请输入修改后密码:");
        String password = scan.nextLine();
        System.out.println("请输入修改后昵称:");
        String nickname = scan.nextLine();
        System.out.println("请输入修改后年龄:");
        int age = scan.nextInt();
        try (Connection conn = DBUtil.getConnection()){

            String sql = "UPDATE user "+
                         "SET password=?,nickname=?,age=? "+
                         "WHERE username=?";
            PreparedStatement ps = conn.prepareStatement(sql);
                ps.setString(1,password);
                ps.setString(2,nickname);
                ps.setInt(3,age);
                ps.setString(4,username);
                int num = ps.executeUpdate();
                System.out.println(num>0?"修改成功":"修改失败");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
